package com.itheima.web;


import com.itheima.listener.WebApplicaitonContextUtils;
import com.itheima.service.UserService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * springMVC入门
 * 1导入springMVC坐标
 * 2配置springMVC核心控制器DispathcerServlet
 * 3创建controller类和视图页面
 * 4使用注解配置controller类中业务方法的映射地址
 * 5配置springMVC和兴文件spring-mvc.xml
 */
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet被访问了");
        //这样每次创建效率不高
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //这样在域中取,效率更高
        ServletContext servletContext = this.getServletContext();
//        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
//        ApplicationContext app = WebApplicaitonContextUtils.getWebApplicationContext(servletContext);
        //因为tomcat10的jakarta.servlet改变,不能导包运行
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
        resp.getWriter().write("dslaa");

    }
}
